package com.logisticsinquiry.android

import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.logisticsinquiry.android.QueryApplication.Companion.context
import com.logisticsinquiry.android.logic.dao.UserDao
import com.logisticsinquiry.android.logic.database.AppDatabase
import com.logisticsinquiry.android.logic.entity.User
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.history.*
import kotlin.concurrent.thread

class LoginActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        // 设置输入框的drawableLeft
        val usernameImg: Drawable? = ContextCompat.getDrawable(QueryApplication.context, R.drawable.username)
        val passwordImg: Drawable? = ContextCompat.getDrawable(QueryApplication.context, R.drawable.password)

        usernameImg?.setBounds(
            0,
            0,
            70,
            70
        )
        passwordImg?.setBounds(
            0,
            0,
            70,
            70
        )
        input_username.setCompoundDrawables(usernameImg, null, null, null)
        input_password.setCompoundDrawables(passwordImg, null, null, null)

        // 记住密码
        val prefs = getPreferences(Context.MODE_PRIVATE)
        val isRemember = prefs.getBoolean("remeber_password",false)
        if (isRemember){
            val account =prefs.getString("account","")
            val password = prefs.getString("password","")
            input_username.setText(account)
            input_password.setText(password)

            remember.isChecked = true
        }

        
        
        //登录
        button_login.setOnClickListener {
            val userDao = AppDatabase.getDatabase(QueryApplication.context).userDao()
            if(userDao.loadUserByUsername(input_username.text.toString()) != null && input_password.text.toString().equals(userDao.loadUserByUsername(input_username.text.toString()).password)){
                Log.v("数据库随便康康",input_username.text.toString())

                // 记住密码
                val editor = prefs.edit()
                if (remember.isChecked){
                    editor.putBoolean("remeber_password",true)
                    editor.putString("account",input_username.text.toString())
                    editor.putString("password",input_password.text.toString())
                }else{
                    editor.clear()
                }
                editor.apply()

                // 跳转
                val intent = Intent(context, MainActivity::class.java).apply {
                    putExtra("username",input_username.text.toString())
                }
                startActivity(intent)
                /*this.finish()*/
            } else {
                if(userDao.loadUserByUsername(input_username.text.toString()) == null ){
                    val user = User(input_username.text.toString(),input_password.text.toString())

                    // 记住密码
                    val editor = prefs.edit()
                    if (remember.isChecked){
                        editor.putBoolean("remeber_password",true)
                        editor.putString("account",input_username.text.toString())
                        editor.putString("password",input_password.text.toString())
                    }else{
                        editor.clear()
                    }
                    editor.apply()

                    // 插入数据
                    userDao.insertUser(user)

                    // 页面跳转
                    val intent = Intent(context, MainActivity::class.java).apply {
                        putExtra("username",input_username.text.toString())
                    }
                    startActivity(intent)
                /*this.finish()*/
                } else {
                    Log.v("数据库随便康康",userDao.loadUserByUsername(input_username.text.toString()).toString())
                    Toast.makeText(context,"数据库中已存在相同用户名，密码错误，拒绝登录",Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}